<template>
  <div>
    <!-- 复选框 -->
    <el-checkbox-group v-model="inputValue" :disabled="disabled" :max="max ? max : dataOptions.length" size="small">
      <el-checkbox v-for="(value, key) in dataOptions" :key="key" :label="key" :name="`checkbox${key}`">{{
        value
      }}</el-checkbox>
    </el-checkbox-group>
  </div>
</template>

<script>
export default {
  name: "TCheckbox",
  model: {
    prop: "value",
    event: "change",
  },
  props: {
    value: {
      type: [Array],
      default: () => {
        return [];
      },
    },
    /**
     * 字典标识，默认 性别
     */
    dictCode: {
      type: String,
      default: "gender",
    },
    /**
     * 禁用状态
     */
    disabled: {
      type: Boolean,
      default: false,
    },
    /**
     * 可被勾选的 checkbox 的最大数量
     */
    max: {
      type: [Number],
      default: 0,
    },
  },
  data() {
    return {};
  },
  computed: {
    // 选中值
    inputValue: {
      get() {
        return this.value;
      },
      set(val) {
        const labels = val.map((key) => {
          return this.dataOptions[key];
        });
        this.$emit("change", val, labels);
      },
    },
    // 选择器数据源
    dataOptions() {
      return this.$store.state.data.KV_MAP[this.dictCode] || [];
    },
  },
};
</script>

<style lang="scss" scoped></style>
